//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Packaging;

namespace Microsoft.Research.DataLayer
{
    /// <summary>
    /// 
    /// </summary>
    public class OPCXomlHandler
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        /// <param name="objId"></param>
        /// <param name="paramList"></param>
        /// <param name="objKind"></param>
        /// <param name="container"></param>
        /// <param name="partType"></param>
        public static void InsertXomlRecord(Connection.Storage obj1, Connection.Storage obj2,
            object objId, ref List<Parameter> paramList, string objKind, ref Package container,
            string partType)
        {
            //string objKind = ObjectKind(obj1, obj2);

            Parameter contentsParam = Parameter.GetParamByName("XomlContents", paramList);
            Parameter nameParam = Parameter.GetParamByName("Name", paramList);
            if (contentsParam == null || contentsParam.Value == null)
                return;
            string fileName = (nameParam == null || nameParam.Value == null) ? "WF.xoml" : nameParam.Value.ToString();
            string elemUri = String.Concat("/Trident/" + objKind + "/", objId.ToString() + "/" + fileName);
            elemUri = elemUri.Replace(' ', '_');

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] data = encoding.GetBytes(contentsParam.Value.ToString());

            PackagePart part = container.CreatePart(new Uri(elemUri, UriKind.Relative), partType);
            part.GetStream().Write(data, 0, data.Length);

            container.Flush();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="paramList"></param>
        /// <param name="objKind"></param>
        /// <param name="container"></param>
        /// <param name="partType"></param>
        public static void UpdateXomlRecord(Connection.Storage obj, List<Parameter> paramList,
            string objKind, ref Package container, string partType)
        {
            Parameter idParam = Parameter.GetParamByName("ID", paramList);
            Parameter contentsParam = Parameter.GetParamByName("XomlContents", paramList);
            Parameter nameParam = Parameter.GetParamByName("Name", paramList);
            if (contentsParam == null || contentsParam.Value == null)
                return;

            //string objKind = ObjectKind(obj);

            string fileName = (nameParam == null || nameParam.Value == null) ? "WF.xoml" : nameParam.Value.ToString();
            fileName = fileName.Replace(' ', '_');
            Uri elemUri = new Uri(String.Concat("/Trident/" + objKind + "/", idParam.Value.ToString() + "/" + fileName), UriKind.Relative);
            PackagePart xomlFilePart = null;
            if (container.PartExists(elemUri))
                xomlFilePart = container.GetPart(elemUri);
            else
                xomlFilePart = container.CreatePart(elemUri, partType);

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] data = encoding.GetBytes(contentsParam.Value.ToString());

            xomlFilePart.GetStream().Write(data, 0, data.Length);
            container.Flush();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objKind"></param>
        /// <param name="id"></param>
        /// <param name="container"></param>
        public static void DeleteXomlRecord(string objKind, Guid id, ref Package container)
        {
            string elemUri = String.Concat("/Trident/" + objKind + "/", id.ToString() + "/");
            Uri partDeleteUri = null;

            // Since the file name is not passed as part of the arguments
            // traverse all the parts and match string to determin the xoml part.
            PackagePartCollection parts = container.GetParts();
            foreach (PackagePart part in parts)
            {
                if (part.Uri.OriginalString.StartsWith(elemUri))
                {
                    partDeleteUri = part.Uri;
                    break;
                }
            }
            container.DeletePart(partDeleteUri);
        }
    }
}
